//
//  EFQuickQcodeOrderView.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/20.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFQuickQcodeOrderView.h"
#import "EFSepecialItem.h"
#import "EFSaleView.h"
#import "EFQuickQcodeProductView.h"
#import "EFShoppingCartViewController.h"

@interface EFQuickQcodeOrderView ()
@property (strong, nonatomic) UILabel *countLabel;
@property (strong, nonatomic) UIButton *editBtn;
@property (strong, nonatomic) UIScrollView *productScrollView;
@property (strong, nonatomic) NSMutableArray *productArray;

@end

@implementation EFQuickQcodeOrderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.countLabel];
    [self addSubview:self.editBtn];
    self.editBtn.right = kScreenWidth - 20;
    self.editBtn.top = 12;
    [self addSubview:self.productScrollView];
    self.productScrollView.top = 52;
}

- (void)setObject:(NSMutableArray *)itemArray{
    for (UIView *view in self.productScrollView.subviews) {
        [view removeFromSuperview];
    }
    self.countLabel.text = [NSString stringWithFormat:@"我的购物车(%lu)",(unsigned long)itemArray.count];
    [self.countLabel sizeToFit];
    self.countLabel.left = 20;
    self.countLabel.top = 12;
    
    CGFloat x = 20;
    CGFloat y = 0;
    for (EFSepecialItem *item in itemArray) {
        EFQuickQcodeProductView *view = [[EFQuickQcodeProductView alloc] initWithFrame:CGRectMake(x, y, 80, 80) Item:item];
        x += 80 + 9;
        [self.productScrollView addSubview:view];
    }
    self.productScrollView.contentSize = CGSizeMake(20 + itemArray.count * 80 + (itemArray.count - 1)*9, 80);
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.font = [UIFont systemFontOfSize:13];
        _countLabel.textColor = kBlack999;
        _countLabel.text = @"我的购物车";
        [_countLabel sizeToFit];
        _countLabel.left = 20;
        _countLabel.top = 12;
    }
    return _countLabel;
}

- (UIButton *)editBtn{
    if (!_editBtn) {
        _editBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 17)];
        [_editBtn setTitle:@"展开" forState:UIControlStateNormal];
       // [_editBtn setImage:IMG(@"edit_icon_default") forState:UIControlStateNormal];
        _editBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_editBtn setTitleColor:kBlack999 forState:UIControlStateNormal];
       // _editBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 0);
        [_editBtn addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editBtn;
}

- (UIScrollView *)productScrollView{
    if (!_productScrollView) {
        _productScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 151)];
        _productScrollView.backgroundColor = [UIColor whiteColor];
    }
    return _productScrollView;
}

- (void)edit{
    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
        EFShoppingCartViewController *shopcart = [[EFShoppingCartViewController alloc] initWithType:1];
        [ICNavigator pushViewController:shopcart animated:YES];
    }else{
        [ICNavigator openLoginWithCompleteBlock:^{
            //二维码 update一下
            
            
        }];
    }
   
   }
@end
